package org.csu.mypetstore.web.servlet;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.UserCart;
import org.csu.mypetstore.service.UserCartService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ViewMyCartsServlet", value = "/ViewMyCartsServlet")
public class ViewMyCartsServlet extends HttpServlet {
    private static String USER_CART="/WEB-INF/jsp/cart/ViewCart.jsp";
    private static String MAIN="/WEB-INF/jsp/SignonForm.jsp";
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        List<UserCart> userCartList=new ArrayList<UserCart>();
        UserCartService service=new UserCartService();
        if(((Account)session.getAttribute("accounts")).getUsername()!=null)
        {
            userCartList=service.getCartListByUserid(((Account)session.getAttribute("accounts")).getUsername());
            session.setAttribute("userCartList",userCartList);
            request.getRequestDispatcher(USER_CART).forward(request,response);
        }
        else
        {
            session.setAttribute("msg","您并未登陆");
            request.getRequestDispatcher(MAIN).forward(request,response);

        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
